<?php

namespace App\Console\Commands;

use App\Contract\ContractStatus;
use App\Http\Service\DeviceService;
use App\Model\Bill;
use App\Model\Contract;
use App\Model\DeviceLog;
use Illuminate\Console\Command;

class DeviceAutoStopCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'device:auto-stop';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '设备自动断电';

    protected $deviceLog = [];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 找出所有预期账单
        /*$bills = Bill::query()->where('is_close', 0)
            ->where('pay_date', '<', now()->toDateString())
            ->where('is_paid', 0)
            ->orderByDesc('id')
            ->with('business', 'business.conf', 'contract', 'house', 'house.device')
            ->get();
        foreach ($bills as $bill)
        {
            $isClose = false;
            // 合同是否设置了逾期自动断电
            if ($bill->contract->is_fail_close) {
                $isClose = true;
            }
            // 房东总配置表是否设设置了
            if ($bill->business && $bill->business->conf && $bill->business->conf->is_fail_close) {
                $isClose = true;
            }
            if ($isClose && $bill->house && $bill->house->device) {
                foreach ($bill->house->device as $d) {
                    $this->handleDeviceClose($d->mac);
                }
            }
        }*/

        $data = Contract::query()->where('status', ContractStatus::FASTQY)
            ->where('expired_at', '<', now()->toDateString())
            ->where('is_fail_close', 1)
            ->with('house', 'house.device', 'business', 'business.conf')
            ->get();
        foreach ($data as $item) {
//            $isClose = false;
//            // 合同是否设置了逾期自动断电
//            if ($item->is_fail_close) {
//                $isClose = true;
//            }
//            // 房东总配置表是否设设置了
//            if ($item->business && $item->business->conf && $item->business->conf->is_fail_close) {
//                $isClose = true;
//            }
            $isClose = true;
            if ($isClose && $item->house && $item->house->device) {
                foreach ($item->house->device as $d) {
                    if($d->online_status == 1){
                        DeviceLog::create([
                            'action' => 3,
                            'device_id' => $item->house->device->id,
                            'log' => $item->house->device->sn.'逾期拉闸',
                            'amount' => 0,
                            'user_name' => '自动拉闸'
                        ]);
                        $this->handleDeviceClose($d->mac);
                    }
//                    else{
//                        DeviceLog::create([
//                            'action' => 3,
//                            'device_id' => $item->house->device->id,
//                            'log' => $item->house->device->sn.'逾期拉闸时设备无信号',
//                            'amount' => 0,
//                            'user_name' => '自动拉闸'
//                        ]);
//                    }
                }
            }
        }

    }

    public function handleDeviceClose($mac)
    {

        $service = app()->make(DeviceService::class);
        $service->stop($mac);
    }
}
